package com.pactera.mavenweb.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pactera.mavenweb.dao.UserRoleDao;
import com.pactera.mavenweb.entity.UserRole;
import com.pactera.mavenweb.service.UserRoleService;

@Service("userRoleService")
public class UserRoleServiceImpl implements UserRoleService {
	
	/**
	 * 注入dao
	 */
	@Resource(name = "userRoleDao")
	private UserRoleDao userRoleDao;
	
//	@Override
	public int saveUserRoles(List<UserRole> userRoles) {
		try {
			for (UserRole userRole : userRoles) {
				userRoleDao.saveUserRole(userRole);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return 0;
	}
	
//	@Override
	public List<UserRole> getAllUserRole() {
		return userRoleDao.queryAll();
	}
	
//	@Override
	public void addUserRole(UserRole userRole) {
		userRoleDao.saveUserRole(userRole);
	}
	
//	@Override
	public boolean delUserRole(int id) {
		
		userRoleDao.delete(id);
		
		return true;
	}
	
//	@Override
	public void updateUserRole(UserRole userRole) {
		userRoleDao.update(userRole);
	}
	
//	@Override
	public UserRole getUserRole(String id) {
		return userRoleDao.queryById(id);
	}
	
//	@Override
	public List<UserRole> getRolesFromUserId(int userId) {
		String hql = "from UserRole where user_id = ?";
		Object[] idArray = {userId};
		
		return userRoleDao.queryForList(hql, idArray);
	}
}